Imports System.Xml.Serialization


''' <summary>
''' Represents the project references for a VBA project
''' </summary>
''' <remarks></remarks>
Public Class VBAProjectReferences
    Inherits System.Collections.Generic.List(Of VBAReference)

#Region "Constructors"

    ''' <summary>
    ''' Default constructor
    ''' </summary>
    Public Sub New()
        MyBase.New()
    End Sub

    ''' <summary>
    ''' Constructor that automatically populates from a source VBA project
    ''' </summary>
    ''' <param name="sourceProject">The project to populate the references from</param>
    Public Sub New(ByVal sourceProject As Microsoft.Vbe.Interop.VBProject)
        Me.New()

        ' Extract the references
        Me.ExtractReferences(sourceProject)


    End Sub

#End Region

#Region "VBE Operations"


    ''' <summary>
    ''' Extracts all the references from a given project file
    ''' </summary>
    ''' <param name="sourceProject">The project to populate the references from</param>
    ''' <remarks></remarks>
    Public Sub ExtractReferences(ByVal sourceProject As Microsoft.Vbe.Interop.VBProject)

        ' Loop around all the references and add them to me
        For Each ref As Microsoft.Vbe.Interop.Reference In sourceProject.References
            Me.Add(New VBAReference(ref))
        Next

    End Sub

    ''' <summary>
    ''' Adds all the references we contain back to a VBA project
    ''' </summary>
    ''' <param name="destProject">The project to add the references to</param>
    ''' <remarks></remarks>
    Public Sub AddReferencesToVBAProject(ByVal destProject As Microsoft.Vbe.Interop.VBProject)

    End Sub



#End Region



End Class


''' <summary>
''' Represents a reference inside a VBA project
''' </summary>
Public Class VBAReference

    ' To represent a reference, all we have to do is store the guid and version.
    Private _Guid As System.Guid
    Private _MajorVer As Integer
    Private _MinorVer As Integer

#Region "Constructors"

    ''' <summary>
    ''' Default Constructor
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
        MyBase.New()
    End Sub

    ''' <summary>
    ''' Creates the class from a VBE reference object
    ''' </summary>
    ''' <param name="ref">The vbe reference object to initialize from</param>
    Public Sub New(ByVal ref As Microsoft.Vbe.Interop.Reference)

        ' Populate from the reference
        Me.populate(ref)

    End Sub

#End Region


    ' Populate from the reference
    Private Sub Populate(ByVal ref As Microsoft.Vbe.Interop.Reference)

        _Guid = New System.Guid(ref.Guid)
        _MajorVer = ref.Major
        _MinorVer = ref.Minor

    End Sub


    ' Adds to a VBA Project
    Public Sub AddToVBAProject(ByVal destProject As Microsoft.Vbe.Interop.VBProject)

        destProject.References.AddFromGuid(_Guid.ToString, _MajorVer, _MinorVer)

    End Sub

End Class